<?php
/**
 * DashboardController.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-03-10 09:32:22
 * @modified    2023-03-10 09:32:22
 *
 */

namespace App\Http\Controllers;

use App\Services\DashboardService;
use Darabonba\GatewaySpi\Models\InterceptorContext\request;

class DashboardController extends Backend
{
    public function __construct()
    {
        parent::__construct();
        $this->service = new DashboardService();
    }

    /**
     * @OA\Get(
     *     path="/dashboard",
     *     tags={"Dashboard"},
     *     summary="获取服务台所有信息",
     *     description="获取服务台所有信息",
     *     operationId="dashboard_index",
     *     security={{"bearerAuth":{}}},
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function index()
    {
        try {
            $data = $this->service->all();
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/dashboard/close_probability",
     *     tags={"Dashboard"},
     *     summary="获取及时关单率",
     *     description="获取及时关单率",
     *     operationId="dashboard_close_probability",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="type", in="query", description="时间类型(week, month, quarter, year, month_select), 分别表示本周、本月、本季度、本年，选择月份。默认为选择月份", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="month_start", in="query", description="开始月份，当type为month_select时的参数，格式为: xxxx-xx, 默认为当前月", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="month_end", in="query", description="结束月份，当type为month_select时的参数，格式为: xxxx-xx，默认为当前月", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function closeProbability()
    {
        $type = request()->get('type');
        $monthStart = request()->get('month_start') ?? '';
        $monthEnd = request()->get('month_end') ?? '';
        if (!in_array($type, [DashboardService::DATE_TYPE_YEAR, DashboardService::DATE_TYPE_QUARTER, DashboardService::DATE_TYPE_MONTH, DashboardService::DATE_TYPE_WEEK, DashboardService::DATE_TYPE_MONTH_SELECT])) {
            $type = DashboardService::DATE_TYPE_MONTH_SELECT;
        }
        try {
            $data = $this->service->closeProbability($type, $monthStart, $monthEnd);
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/dashboard/person_efficiency",
     *     tags={"Dashboard"},
     *     summary="获取服务人效",
     *     description="获取服务人效",
     *     operationId="dashboard_person_efficiency",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="type", in="query", description="时间类型(week, month, quarter, year, month_select), 分别表示本周、本月、本季度、本年，选择月份。默认为选择月份", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="month_start", in="query", description="开始月份，当type为month_select时的参数，格式为: xxxx-xx, 默认为当前月", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="month_end", in="query", description="结束月份，当type为month_select时的参数，格式为: xxxx-xx，默认为当前月", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function personEfficiency()
    {
        $type = request()->get('type');
        $monthStart = request()->get('month_start') ?? '';
        $monthEnd = request()->get('month_end') ?? '';
        if (!in_array($type, [DashboardService::DATE_TYPE_YEAR, DashboardService::DATE_TYPE_QUARTER, DashboardService::DATE_TYPE_MONTH, DashboardService::DATE_TYPE_WEEK, DashboardService::DATE_TYPE_MONTH_SELECT])) {
            $type = DashboardService::DATE_TYPE_MONTH_SELECT;
        }
        try {
            $data = $this->service->personEfficiency($type, $monthStart, $monthEnd);
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/dashboard/todo",
     *     tags={"Dashboard"},
     *     summary="待办事项",
     *     description="待办事项",
     *     operationId="dashboard_todo",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="type", in="query", description="all、creator、assist、master 分别表示 全部、我创建的、我协同的、我负责的, 默认为全部", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function todo()
    {
        $type = \request()->get('type') ?? '';
        $types = $this->service->types();
        if (!in_array($type, $types)) {
            $type = $this->service::TYPE_ALL;
        }
        try {
            $todo = $this->service->todo($type);
            return message('获取成功', true, $todo);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/dashboard/satisfaction_form",
     *     tags={"Dashboard"},
     *     summary="获取客户满意度构成",
     *     description="获取客户满意度构成",
     *     operationId="dashboard_satisfaction_form",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="type", in="query", description="时间类型(week, month, quarter, year, month_select), 分别表示本周、本月、本季度、本年，选择月份。默认为选择月份", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="month_start", in="query", description="开始月份，当type为month_select时的参数，格式为: xxxx-xx, 默认为当前月", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="month_end", in="query", description="结束月份，当type为month_select时的参数，格式为: xxxx-xx，默认为当前月", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function satisfactionForm()
    {
        $type = request()->get('type');
        $monthStart = request()->get('month_start') ?? '';
        $monthEnd = request()->get('month_end') ?? '';
        if (!in_array($type, [DashboardService::DATE_TYPE_YEAR, DashboardService::DATE_TYPE_QUARTER, DashboardService::DATE_TYPE_MONTH, DashboardService::DATE_TYPE_WEEK, DashboardService::DATE_TYPE_MONTH_SELECT])) {
            $type = DashboardService::DATE_TYPE_MONTH_SELECT;
        }
        try {
            $data = $this->service->satisfactionForm($type, $monthStart, $monthEnd);
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/dashboard/avg_service_time",
     *     tags={"Dashboard"},
     *     summary="获取平均服务用时",
     *     description="获取平均服务用时",
     *     operationId="dashboard_avg_service_time",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="type", in="query", description="时间类型(week, month, quarter, year, month_select), 分别表示本周、本月、本季度、本年，选择月份。默认为选择月份", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="month_start", in="query", description="开始月份，当type为month_select时的参数，格式为: xxxx-xx, 默认为当前月", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="month_end", in="query", description="结束月份，当type为month_select时的参数，格式为: xxxx-xx，默认为当前月", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function avgServiceTime()
    {
        $type = request()->get('type');
        $monthStart = request()->get('month_start') ?? '';
        $monthEnd = request()->get('month_end') ?? '';
        if (!in_array($type, [DashboardService::DATE_TYPE_YEAR, DashboardService::DATE_TYPE_QUARTER, DashboardService::DATE_TYPE_MONTH, DashboardService::DATE_TYPE_WEEK, DashboardService::DATE_TYPE_MONTH_SELECT])) {
            $type = DashboardService::DATE_TYPE_MONTH_SELECT;
        }
        try {
            $data = $this->service->avgServiceTime($type, $monthStart, $monthEnd);
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/dashboard/satisfaction_probability",
     *     tags={"Dashboard"},
     *     summary="获取客户满意度好评率",
     *     description="获取客户满意度好评率",
     *     operationId="dashboard_satisfaction_probability",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="type", in="query", description="时间类型(week, month, quarter, year, month_select), 分别表示本周、本月、本季度、本年，选择月份。默认为选择月份", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="month_start", in="query", description="开始月份，当type为month_select时的参数，格式为: xxxx-xx, 默认为当前月", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="month_end", in="query", description="结束月份，当type为month_select时的参数，格式为: xxxx-xx，默认为当前月", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function satisfactionProbability()
    {
        $type = request()->get('type');
        $monthStart = request()->get('month_start') ?? '';
        $monthEnd = request()->get('month_end') ?? '';
        if (!in_array($type, [DashboardService::DATE_TYPE_YEAR, DashboardService::DATE_TYPE_QUARTER, DashboardService::DATE_TYPE_MONTH, DashboardService::DATE_TYPE_WEEK, DashboardService::DATE_TYPE_MONTH_SELECT])) {
            $type = DashboardService::DATE_TYPE_MONTH_SELECT;
        }
        try {
            $data = $this->service->satisfactionProbability($type, $monthStart, $monthEnd);
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }
}